package com.linzm.creationaltype.prototype05.factory;

import com.linzm.creationaltype.prototype05.enums.ShapeTypeEnum;
import com.linzm.creationaltype.prototype05.shape.Shape;

import java.util.Hashtable;

/**
 * @Author zimingl
 * @Date 2023/4/15 21:04
 * @Description: ShapeCache
 */
public class ShapeCache {

    private static final Hashtable<String, Shape> shapeMap = new Hashtable<>();

    public static Shape getShape(String shapeId) {
        Shape cachedShape = shapeMap.get(shapeId);
        return (Shape) cachedShape.clone();
    }

    /**
     * 对每种形状都运行数据库查询，并创建该形状
     * shapeMap.put(shapeKey, shape);
     * 例如，我们要添加三种形状
     */
    public static void loadCache() throws Exception {
        ShapeFactory shapeFactory = new ShapeFactory();
        shapeMap.put("1", shapeFactory.getShape(ShapeTypeEnum.CIRCLE.name()));
        shapeMap.put("2", shapeFactory.getShape(ShapeTypeEnum.SQUARE.name()));
        shapeMap.put("3", shapeFactory.getShape(ShapeTypeEnum.SQUARE.name()));
    }
}
